
/*************************************************
                    头文件
**************************************************/
// 通用库
#include <cstdlib>

// 项目内部/
#include "Reader.h"
#include "args.hxx"


#define PROJECT ("extract")
#define VERSION (1.0)


void mainProcess(
    const std::string& input_path, 
    const int& id_col,
    const int& description_col,
    const int& safety_col,
    const int& type_col,
    const std::string& output_path){


    XlsxProcessor::Reader r;
    XlsxProcessor::writer w;

    r.load(input_path);
    w.loadSheet("sheet1");

    w.setColumnWidth(1, 30);
    w.setColumnWidth(2, 80);
    w.setColumnWidth(3, 20);
    w.setColumnWidth(4, 20);

    int out_row = 1;
    w.writeString(out_row, 1, "ID");
    w.writeString(out_row, 2, "DESCRIPTION");
    w.writeString(out_row, 3, "SAFETY");
    w.writeString(out_row++, 4, "TYPE");


    for (auto row = 1; row <= r.getRowsCount(); row++) {
        std::vector<XlsxProcessor::DATA_STRUCT> res = r.readLine(row, id_col, description_col, safety_col, type_col);
        for(auto item : res) {
            int col = 1;
            w.writeString(out_row, col++, item.id);
            w.writeString(out_row, col++, item.description);
            w.writeString(out_row, col++, item.safety);
            w.writeString(out_row++, col++, item.type);
        }
    }

    w.save(output_path);

}




int main(int argc, char** argv) {

    args::ArgumentParser parser("该程序用于将安全需求转化成每行一个需求.", "使用示例: ./extract.exe -i D:/Requiremets/demo_input.xlsx [1 2 3 4] -o demo_output.xlsx");
    args::HelpFlag help(parser, "help", "\t\t\t\t\t\t\t\t显示帮助文档", {'h',
         "help"});
    args::Flag version(parser, "Version", "\t\t\t\t\t\t\t\t显示软件版本", {'v', 'V', "version"});
    // args::Flag acceptFlag(parser, "acceptflag", "\t\t\t\t\t\t\t\t接受文档修改并修改为读写模式", {'i', 'I', "input"});
    // args::Flag searchFlag(parser, "searchflag", "\t\t\t\t\t\t\t\t搜索模式", {'s', 'S', "Search"});
    // args::Flag mainFlag(parser, "mainflag", "\t\t\t\t\t\t\t\t主进程标志, 如果时单独进程使用，该参数为必选", {'m', 'M', "Main"});
    args::ValueFlag<std::string> input_path(parser, "输入文档", "输入文件所在路径及名称", {'i', 'I'});
    args::ValueFlag<std::string> output_path(parser, "输出文档", "\t\t\t\t输入文件所在路径及名称", {'o', 'O'});
    args::PositionalList<int> param_list(parser, "params", "\t\t\t\t输入表格的列参数, [id列位置, description列位置, safety列位置, type列位置]");

    try
    {
        parser.ParseCLI(argc, argv);
    }
    catch (args::Help&)
    {
        std::cout << parser;
        return EXIT_SUCCESS;
    }
    catch (args::ParseError e)
    {
        std::cerr << e.what() << std::endl;
        std::cerr << parser;
        return EXIT_FAILURE;
    }
    catch (args::ValidationError e)
    {
        std::cerr << e.what() << std::endl;
        std::cerr << parser;
        return EXIT_FAILURE;
    }

    if (!input_path || !output_path) {
        std::cerr << "输入输出文档参数不能为空" << std::endl;
        std::cerr << parser;
    }

    int id_col = 1, desc_col = 2, safety_col = 3, type_col = 4;

    if (param_list) {
        int index = 0;
        for (const auto& value : args::get(param_list)) {
            switch(index++) {
                case 0:
                id_col = value;
                break;

                case 1:
                desc_col = value;
                break;

                case 2:
                safety_col = value;
                break;

                case 4:
                type_col = value;
                break;

                default:
                break;
            }
        }
    }

    mainProcess(args::get(input_path), id_col, desc_col, safety_col, type_col, args::get(output_path));

    return EXIT_SUCCESS;
}